package com.stu.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.common.R;
import com.stu.pojo.StuA;
import com.stu.pojo.Student;
import com.stu.pojo.User;
import com.stu.pojo.vo.StudentDescVo;
import com.stu.pojo.vo.StudentVo;
import com.stu.service.IStuAService;
import com.stu.service.IStudentDescService;
import com.stu.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@RestController
@RequestMapping("/stuA")
public class StuAController {
    @Autowired
    IStuAService stuAService;
    @Autowired
    IStudentService studentService;
    @Autowired
    IStudentDescService studentDescService;

    @GetMapping("/output")
    public void output(@RequestParam Set<String> includeColumnFiledNames,
                       String name,
                       HttpServletResponse response){
        System.out.println(includeColumnFiledNames);
        response.setHeader("Content-Disposition","attachment;filename="+ UUID.randomUUID()+"stuoa.xlsx");
        LambdaQueryWrapper<StuA> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),StuA::getName,name);
        qw.orderByDesc(StuA::getStatus);
        qw.orderByDesc(StuA::getUpdateTime);
        List<StuA> list = stuAService.list(qw);
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        EasyExcel.write(out, StuA.class).includeColumnFiledNames(includeColumnFiledNames).sheet("模板")
                .doWrite(list);
        System.out.println(list);
    }


    //学生提交表单
    @PostMapping("/submit")
    public R submit(@RequestBody StuA stuA){
        boolean b = stuAService.insertStuA(stuA);
        return b ? R.success(stuA) : R.error("提交失败");
    }

    //审核表单 未通过 -> 已通过 新增两个表
    @PostMapping("/pass")
    public R pass(@RequestBody StudentVo studentVo){
        boolean b = stuAService.pass(studentVo);
        return b ? R.success(studentVo) : R.error("操作失败");
    }

    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name){
        Page<StuA> studentPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<StuA> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),StuA::getName,name);
        qw.orderByDesc(StuA::getStatus);
        qw.orderByDesc(StuA::getUpdateTime);
        stuAService.page(studentPage,qw);
        return R.success(studentPage);
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        StuA stuA = stuAService.getById(id);
        if (stuA != null){
            return R.success(stuA);
        }
        return R.error("没有查询到对应学生信息");
    }

    // 编辑 打回
    @PutMapping
    public R update(@RequestBody StuA stuA){
        boolean flag = stuAService.updateById(stuA);
        return flag ? R.success("修改成功"):R.error("修改失败");
    }
}

